<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>{{ type == 1 ? '更换配送员' : '绑定配送员' }}</p>
        <i @click="closePopup(false)"></i>
      </div>

      <template>
        <div class="box_body">
          <div class="line">
            <span class="lab"><i class="mark_icon">*</i>配送员：</span>
            <div :class="{'error_border':tip_num===1}">
              <el-select
                filterable
                placeholder="请选择配送员"
                v-model="bindSenderId"
                value-key="id">
                <el-option
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                  v-for="(item,index) in senderOptions">
                </el-option>
              </el-select>
            </div>
          </div>
          <p class="error_line">{{ tips }}</p>
        </div>
      </template>

      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirm">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import {getAllData} from "@/api/basic/warehouse/sender";
import {bindSender} from "@/api/data/user/address";

export default {
  props: ['addressId', 'senderId', 'type', 'warehouseId'],
  data() {
    return {
      senderOptions: [],
      loading: false,
      senders: '',
      refereeName: '',
      tip_num: '',
      tips: '',
      bindSenderId: this.senderId ? this.senderId : ''
    }
  },
  mounted() {
    this.getSenderList();
  },
  methods: {
    getSenderList() {
      getAllData({warehouseId: this.warehouseId,isAll:1}).then(res => {
        this.senderOptions = res ? res : [];
      })
    },

    //确定
    confirm() {
      if (!this.bindSenderId) {
        this.tip_num = 1;
        this.tips = '请选择配送员!';
      } else {
        this.tip_num = '';
        this.tips = '';
        this.$confirm(this.type == 1 ?'确定更换该配送员?' : '确定绑定该配送员?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.sendInfo();
        }).catch(() => {
        })
      }

    },
    sendInfo() {
      let queryParams = {
        id: this.addressId,
        senderId: this.bindSenderId
      };
      bindSender(queryParams).then(res => {
        this.closePopup(true);
        let text = '';
        this.type == 1 ? text = '更改成功!' : text = '绑定成功!'
        this.$message.success({
          message: text,
          duration: 1500
        });
      }).catch((e) => {
        this.loading = false;
      })
    },

    // 关闭
    closePopup(refresh) {
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>

</style>
